﻿using System;
using System.Reflection;

namespace AppDomain_Sample {
    /// <summary>
    /// http://stackoverflow.com/questions/4981223/currentdomain-assemblyresolve-not-being-able-resolve-some-assemblies
    /// </summary>
    class Program {
        static void Main(string[] args) {
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(currentDomain_AssemblyResolve);

            Type foo = Type.GetType("LibraryB.Foo, LibraryB, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
            Type fooBase = Type.GetType("LibraryA.FooBase, LibraryA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");

            //AppDomain.CurrentDomain.AssemblyResolve -= currentDomain_AssemblyResolve;

            if (fooBase.IsAssignableFrom(foo)) {
                Console.WriteLine("{0} can be assigned from and instance of {1}", fooBase.Name, foo.Name);
            } else {
                Console.WriteLine("{0} cannot be assigned from and instance of {1}", fooBase.Name, foo.Name);
            }            
        }

        static Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args) {
            string strTempAssmbPath = @"D:\Documents\Repository\Mercurial\utfprlibrary\samples\AppDomain_Sample\" + args.Name.Substring(0, args.Name.IndexOf(",")) + @"\bin\Debug\" + args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll";

            //Load the assembly from the specified path. 					
            //Return the loaded assembly.
            return Assembly.LoadFrom(strTempAssmbPath);
        }
    }
}
